package com.heimo.babyage.ui.framegrid;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.heimo.babyage.R;

public class FramePickerAdapter extends BaseAdapter {
	private static final String TAG = "FramePickerAdapter";
	private Context context;
	private List<Integer> frameList;
	int colorGridColumnWidth;

	public FramePickerAdapter(Context context) {
		this.context = context;
		colorGridColumnWidth = context.getResources().getInteger(R.integer.frameGridColumnWidth);
		initList();
	}
	
	private void initList(){
		frameList = new ArrayList<Integer>();
		frameList.add(0);
		frameList.add(R.drawable.frame_01);
		frameList.add(R.drawable.frame_02);
		frameList.add(R.drawable.frame_03);
		frameList.add(R.drawable.frame_04);
		frameList.add(R.drawable.frame_05);
		frameList.add(R.drawable.frame_06);
		frameList.add(R.drawable.frame_07);
		frameList.add(R.drawable.frame_08);
		frameList.add(R.drawable.frame_09);
		frameList.add(R.drawable.frame_10);
		frameList.add(R.drawable.frame_11);
		frameList.add(R.drawable.frame_12);
		frameList.add(R.drawable.frame_13);
		frameList.add(R.drawable.frame_14);
		frameList.add(R.drawable.frame_15);
		frameList.add(R.drawable.frame_16);
//		frameList.add(R.drawable.frame_17);
		frameList.add(R.drawable.frame_18);
		frameList.add(R.drawable.frame_19);
		frameList.add(R.drawable.frame_20);
		frameList.add(R.drawable.frame_21);
		frameList.add(R.drawable.frame_22);
		frameList.add(R.drawable.frame_23);
	}
	
	public View getView(int pos, View convertView, ViewGroup parent) {
		ImageView imageView;

		// can we reuse a view?
		if (convertView == null) {
			imageView = new ImageView(context);
			// set the width of each color square
			imageView.setLayoutParams(new GridView.LayoutParams(colorGridColumnWidth, colorGridColumnWidth));

		} else {
			imageView = (ImageView) convertView;
		}

		// imageView.setBackgroundColor(frameList.get(position));
		int imgID = getImageIdByPos(pos);
		if(imgID==0){
			int c = this.context.getResources().getColor(R.color.color_white);
			imageView.setBackgroundColor(c);
		}else{
			imageView.setImageResource(imgID);		
		}
		imageView.setId(pos);
		return imageView;
	}

	public int getCount() {
		return frameList.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public int getImageIdByPos(int pos) {
		// LogHelper.log(TAG, "getImageIdByPos:"+pos);
		return frameList.get(pos);
	}
	
	public long getItemId(int pos) {
		return pos;
	}
}
